package com.bilin.coin.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.bilin.coin.R;
import com.luck.picture.lib.tools.ScreenUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;

;

/** 图片加载工具类
 * Created by LG on 2017/3/6.
 */

public class GlideUtils {
    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";


    /**
     * 等比例缩放到屏幕宽
     * @param mContext
     * @param url
     * @param imageView
     */
    public void displayImageFillWidth(final Context mContext, String url, final ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.LOW)
                .skipMemoryCache(true)
                .error(R.color.colorEditTextBg2)
                .placeholder(R.color.colorEditTextBg2)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext).asBitmap().load(url).apply(requestOptions).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                int imageWidth = resource.getWidth();
                int imageHeight = resource.getHeight();
                int height = ScreenUtils.getScreenWidth(mContext) * imageHeight / imageWidth;
                ViewGroup.LayoutParams para = imageView.getLayoutParams();
                para.height = height;
                para.width = ScreenUtils.getScreenWidth(mContext);
                imageView.setImageBitmap(resource);
            }

          /*  @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

            }*/
        });
    }

    public void displayImageEqualWidth(final Context mContext, String url, final ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.LOW)
                .skipMemoryCache(true)
                .error(R.color.colorEditTextBg2)
                .placeholder(R.color.colorEditTextBg2)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext).asBitmap().load(url).apply(requestOptions).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                //int imageWidth = resource.getWidth();
                //int imageHeight = resource.getHeight();
                //int height = ScreenUtils.getScreenWidth(mContext) * imageHeight / imageWidth;
                ViewGroup.LayoutParams para = imageView.getLayoutParams();

                para.width = ScreenUtils.getScreenWidth(mContext);
                para.height =  para.width;
                imageView.setImageBitmap(resource);
            }

          /*  @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

            }*/
        });
    }


    /**
     * 等比例缩放到屏幕宽
     * @param mContext
     * @param url
     * @param imageView
     */
    public void displayImageWidth(final Context mContext, String url, final ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .error(R.color.colorEditTextBg2)
                .placeholder(R.color.colorEditTextBg2)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);

       /* Glide.with(mContext).asBitmap().load(url).apply(requestOptions)
                .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                int width = (ScreenUtils.getScreenWidth(mContext)- DensityUtils.dp2px(mContext, 18))/2 ;

                int imageWidth = resource.getWidth();
                int imageHeight = resource.getHeight();
                int height = ScreenUtils.getScreenWidth(mContext) * imageHeight / imageWidth;
                ViewGroup.LayoutParams para = imageView.getLayoutParams();
                para.height = width;
                para.width = width;
                imageView.setImageBitmap(resource);
            }
        });*/
        Glide.with(mContext)
                .load(url)
                .apply(requestOptions)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        Log.e("Wain","加载失败 errorMsg:"+(e!=null?e.getMessage():"null"));
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(final Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        Log.e("Wain","成功  Drawable Name:"+resource.getClass().getCanonicalName());

                        return false;
                    }
                })
                .into(new ImageViewTarget<Drawable>(imageView) {
                    @Override
                    protected void setResource(@Nullable Drawable resource) {
                        int width = (ScreenUtils.getScreenWidth(mContext)- DensityUtils.dp2px(mContext, 18))/2 ;
                        ViewGroup.LayoutParams para = imageView.getLayoutParams();
                        para.height = width;
                        para.width = width;
                        imageView.setImageDrawable(resource);
                    }
                });
       /* Glide.with(mContext).asBitmap().load(url).apply(requestOptions).into(new CustomViewTarget(imageView) {
            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Object resource, @Nullable Transition transition) {
                int width = (ScreenUtils.getScreenWidth(mContext)- DensityUtils.dp2px(mContext, 18))/2 ;
                ViewGroup.LayoutParams para = imageView.getLayoutParams();
                para.height = width;
                para.width = width;
                imageView.setImageBitmap((Bitmap) resource);
            }

            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }
        });*/

    }

    /**
     * 等比例缩放到屏幕宽
     * @param mContext
     * @param url
     * @param imageView
     */
    public void displayImageWidth(final Context mContext, String url, final ImageView imageView, final int rows) {
        Glide.with(mContext).asBitmap().load(url).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                int width = ScreenUtils.getScreenWidth(mContext)/rows /*- DensityUtils.dp2px(mContext, 30)*/;

              /*  int imageWidth = resource.getWidth();
                int imageHeight = resource.getHeight();
                int height = ScreenUtils.getScreenWidth(mContext) * imageHeight / imageWidth;*/
                ViewGroup.LayoutParams para = imageView.getLayoutParams();
                para.height = width;
                para.width = width;
                imageView.setImageBitmap(resource);
            }

          /*  @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

            }*/
        });
    }

    private static class ImageLoaderHolder {
        private static final GlideUtils INSTANCE = new GlideUtils();

    }

    private GlideUtils() {
    }

    public static final GlideUtils getInstance() {
        return ImageLoaderHolder.INSTANCE;
    }

    /**
     * 验证码图片加载
     * @param context
     * @param
     * @param imageView
     */
    public void displayVerificationCodeImage(Context context, Bitmap bitmap, ImageView imageView)
    {
        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
        byte[] bytes = outputStream.toByteArray();

        Glide
                .with(context)
                .load(bytes)
                .transition(DrawableTransitionOptions.withCrossFade())//淡入淡出
                /*.error(R.mipmap.yzm_no)
                .placeholder(R.mipmap.yzm_no)*/
                .into(imageView);
    }

    /**
     * 有长方形站位图，并且加载网络图片
     * @param context
     * @param url
     * @param imageView
     */
    public void displayRectangleImage(Context context, String url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .error(R.color.colorEditTextBg2)
                .placeholder(R.color.colorEditTextBg2)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide
                .with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }

    /**
     * 有长方形站位图，并且加载网络图片
     * @param context
     * @param url
     * @param imageView
     */
    public void displayRectangleImagePlus(Context context, String url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
            /*    .error(R.mipmap.merchant_upload_pic)
                .placeholder(R.mipmap.merchant_upload_pic)*/
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide
                .with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }



    /**
     * 有正方形站位图，并且加载网络图片
     * @param context
     * @param url
     * @param imageView
     */
    public void displaySquareImage(Context context, String url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .error(R.color.colorEditTextBg2)
                .placeholder(R.color.colorEditTextBg2)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide
                .with(context)
                .load(url)
                .apply(requestOptions)
               /* .error(R.mipmap.zwt)
                .placeholder(R.mipmap.zwt)*/
                .into(imageView);
    }

    /**
     * 加载积分的占位图，并且加载网络图片
     * @param context
     * @param url
     * @param imageView
     */
    public void displayCurrencyImage(Context context, String url, ImageView imageView){
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
               /* .error(R.mipmap.loading_pic)
                .placeholder(R.mipmap.loading_pic)*/
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide
                .with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }


    //加载网络图片并设置大小
    public void displayCurrencyImage(Context context, String url, ImageView imageView, int width, int height) {

        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .override(width, height)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }





    //直接加载网络图片
    public void displayImage(Context context, String url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .error(R.color.bg_main_gray)
                .placeholder(R.color.bg_main_gray)
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide
                .with(context)
                .load(url)
                .apply(requestOptions)
                .into(imageView);
    }






    //加载网络图片显示为圆形图片
    public void displayCircleImage(Context context, String url, ImageView imageView) {

        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .bitmapTransform(new CircleCrop());

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

//================================================4.8.0=======================================================
    /**
     * 默认加载方式
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadImage(Context context, String url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate();

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    /**
     * 加载圆形图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadCircleImage(Context context, String url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .dontAnimate()
             /*   .error(R.mipmap.merchant_entry_logo)
                .placeholder(R.mipmap.merchant_entry_logo)*/
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .bitmapTransform(new CircleCrop());

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    public static void loadCircleImageLocal(Context context, int url, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .dontAnimate()
                .error(R.color.bg_main_gray)
                .placeholder(R.color.bg_main_gray)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .bitmapTransform(new CircleCrop());

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    /**
     * 加载圆角图片
     * @param context
     * @param url
     * @param imageView
     * @param radius 圆角大小
     */
    public void loadRoundImage(Context context, String url, ImageView imageView, int radius) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transforms(new CenterCrop(), new RoundedCorners(radius));

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    /**
     * 加载圆角图片
     * @param context
     * @param url
     * @param imageView
     * @param radius 圆角大小
     */
    public void loadRoundImageCard(Context context, String url, ImageView imageView, int radius) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .dontAnimate()
                .override((ScreenUtils.getScreenWidth(context)-DensityUtils.dip2px(context,40))/3, DensityUtils.dip2px(context,95))
               /* .error(R.mipmap.upload_front)
                .placeholder(R.mipmap.upload_front)*/
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transforms(new CenterCrop(), new RoundedCorners(radius));

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    /**
     * 加载图片指定大小
     *
     * @param context
     * @param url
     * @param imageView
     * @param width
     * @param height
     */
    public static void loadSizeImage(Context context, String url, ImageView imageView, int width, int height) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .override(width, height)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    /**
     * 加载本地图片文件
     *
     * @param context
     * @param file
     * @param imageView
     */
    public static void loadFileImage(Context context, File file, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
                .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop();

        Glide.with(context)
                .load(file)
                .apply(requestOptions)
                .into(imageView);
    }

    /**
     * 加载高斯模糊
     *
     * @param context
     * @param url
     * @param imageView
     * @param radius      模糊级数 最大25
     */
    public static void loadBlurImage(Context context, String url, ImageView imageView, int radius) {
        RequestOptions requestOptions = new RequestOptions()
                .override(300)
                .transforms(new BlurTransformation(radius));

        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(imageView);
    }

    /**
     * 加载gif图
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadGifImage(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(url)
                .into(imageView);
    }

    /**
     * 加载gif图
     * @param context
     * @param id
     * @param imageView
     */
    public static void loadGifImage(Context context, int id, ImageView imageView) {
        RequestOptions requestOptions = new RequestOptions()
               /* .error(R.mipmap.remain_bg)
                .placeholder(R.mipmap.remain_bg)*/;
        Glide.with(context).asGif().apply(requestOptions).load(id).into(imageView);
    }

}
